package com.hxexp.weixin.mvc.resetpwd.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Service;

import com.hxexp.weixin.common.exception.APIException;
import com.hxexp.weixin.common.exception.ServiceException;
import com.hxexp.weixin.common.math.RandomUtil;
import com.hxexp.weixin.common.string.StringHelper;
import com.hxexp.weixin.constant.CacheName;
import com.hxexp.weixin.constant.Errcode;
import com.hxexp.weixin.hpe.api.resetpwd.ResetPwdAPI;
import com.hxexp.weixin.hpe.api.resetpwd.model.ResetPwdParamBean;
import com.hxexp.weixin.hpe.api.resetpwd.model.ResetPwdResultBean;
import com.hxexp.weixin.hpe.api.sendcaptcha.SendCaptchaAPI;
import com.hxexp.weixin.hpe.api.sendcaptcha.model.SendCaptchaParamBean;
import com.hxexp.weixin.hpe.api.sendcaptcha.model.SendCaptchaResultBean;
import com.hxexp.weixin.mvc.resetpwd.service.ResetPwdService;

/**
 * 重置密码服务实现类
 *
 * @author wang.bin
 * @author li.jiang
 */
@Service
public final class ResetPwdServiceImpl implements ResetPwdService {
    @Autowired
    private SendCaptchaAPI sendCaptchaAPI;
    @Autowired
    private ResetPwdAPI resetPwdAPI;
    @Autowired
    private EhCacheCacheManager ehCacheManager;

    @Override
    public SendCaptchaResultBean sendCaptcha(String username) throws ServiceException {
        SendCaptchaParamBean sendCaptchaParam = new SendCaptchaParamBean();
        sendCaptchaParam.setUsername(username);
        SendCaptchaResultBean result = new SendCaptchaResultBean();

        // 生成验证码放入缓存
        String captcha = RandomUtil.getRandomNumber(6);
        Cache captchaCache = ehCacheManager.getCache(CacheName.HPE_RESETPWD_CAPTCHA);
        captchaCache.put(username, captcha);
        sendCaptchaParam.setCaptcha(captcha);

        try {
            result = sendCaptchaAPI.sendCaptcha(sendCaptchaParam);
            result.setMobile(StringHelper.hiddenPhoneNumber(result.getMobile()));
        } catch (APIException e) {
            throw new ServiceException(e);
        }
        return result;
    }

    @Override
    public ResetPwdResultBean resetPwd(String captcha, String username) throws ServiceException {
        ResetPwdParamBean resetPwd = new ResetPwdParamBean();
        resetPwd.setCaptcha(captcha);
        resetPwd.setUsername(username);
        ResetPwdResultBean resetPwdresult = new ResetPwdResultBean();

        // 检查验证码是否失效
        Cache captchaCache = ehCacheManager.getCache(CacheName.HPE_RESETPWD_CAPTCHA);
        String captchaFromCache = captchaCache.get(username, String.class);
        // 验证码未失效
        if (captchaFromCache != null) {
            // 输入的验证码和缓存中的验证码一致,
            if (captchaFromCache.equals(captcha)) {
                // NOSONAR
            } else {
                // 输入的验证码和缓存中的验证码不一致
                resetPwdresult.setErrcode(Errcode.CAPTCHA_ERROR);
                resetPwdresult.setErrmsg(Errcode.getMessage(Errcode.CAPTCHA_ERROR));
                return resetPwdresult;
            }
        } else {
            // 验证码已失效
            resetPwdresult.setErrcode(Errcode.CAPTCHA_INVALID);
            resetPwdresult.setErrmsg(Errcode.getMessage(Errcode.CAPTCHA_INVALID));
            return resetPwdresult;
        }

        try {
            resetPwdresult = resetPwdAPI.resetPwd(resetPwd);
        } catch (APIException e) {
            throw new ServiceException(e);
        }
        return resetPwdresult;
    }
}
